package org.kim.service;

import org.kim.dao.BookDao;
import org.kim.dao.BorrowDao;
import org.kim.pojo.Account;
import org.kim.pojo.Book;
import org.kim.pojo.Borrow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class BookService {

    BookDao bookDao = new BookDao();
    BorrowDao borrowDao = new BorrowDao();

    public boolean bookIsBorrowed(Book book) throws Exception {
        List<Book> bookList = bookDao.searchBook(book.getName(), book.getAuthor());
        if (bookList != null && bookList.size() > 0) {
            Book exists = bookList.get(0);
            book.setId(exists.getId());
            return borrowDao.bookIsBorrowed(book);
        }
        return false;
    }

    public Map<String,Book> addBook(Book book) throws Exception {
        Map<String,Book> result=new HashMap<String,Book>();
        List<Book> bookList = bookDao.searchBook(book.getName(), book.getAuthor());
        if (bookList != null && bookList.size() > 0) {
            Book exists = bookList.get(0);
            exists.setInventory(exists.getInventory()+book.getInventory());
            bookDao.updateBook(exists);
            result.put("update",exists);
            return result;
        }else{
            bookDao.addBook(book);
            result.put("add",book);
            return result;
        }
    }

    public boolean updateBook(Book book) throws Exception {
        return bookDao.updateBook(book);
    }

    public List<Book> searchBook(String bookName, String author) {
        return bookDao.searchBook(bookName, author);
    }

    public List<Book> bookLists() {
        return bookDao.bookLists();
    }

    public boolean deleteBook(Book book) throws Exception {
        return bookDao.deleteBook(book);
    }

    public boolean borrowBook(Book book, Account account) throws Exception {
        Borrow borrow = new Borrow();
        borrow.setId(UUID.randomUUID().toString().substring(0, 8));
        //search bookId by bookName,author
        List<Book> bookList = bookDao.searchBook(book.getName(), book.getAuthor());
        if (bookList != null && bookList.size() > 0) {
            Book book1 = bookList.get(0);
            if (book1.getInventory() > 0) {
                borrow.setBookId(book1.getId());
                book1.setInventory(book1.getInventory() - 1);
                borrow.setAccountId(account.getAccountId());
                borrow.setStatus("Borrowing");
                borrowDao.addBorrow(borrow);
                bookDao.updateBook(book1);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public boolean returnBook(Book book, Account account) throws Exception {
        Borrow borrow = new Borrow();
        //search bookId by bookName,author
        List<Book> bookList = bookDao.searchBook(book.getName(), book.getAuthor());
        if (bookList != null && bookList.size() > 0) {
            Book book1 = bookList.get(0);
                borrow.setBookId(book1.getId());
                borrow.setAccountId(account.getAccountId());
                borrow.setStatus("Returned");
                borrowDao.updateBorrow(borrow);
                book1.setInventory(book1.getInventory() + 1);
                bookDao.updateBook(book1);
                return true;
        }
        return false;
    }
}
